package com.javasea.nio.checksum;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 *@ClassName checkSumDemo
 *@Description TODO
 *@Author longxiaonan@163.com
 *@Date 2021/1/24 0024 21:23
 */
public class checkSumDemo {

    public static void main(String[] args) throws JsonProcessingException {
//        test1();
        Command c = new Command();
        c.setId("a11333334");
        c.setName("longxiaonan");
        c.setCmdType("print");
        c.setCheckSum(CheckSumUtil.getCheckSum(c));

        Integer checkSum = c.getCheckSum();
        c.setCheckSum(null);
        boolean b = CheckSumUtil.checkSum(c, checkSum);
        System.out.println(b);
    }

    private static boolean aa(Object obj) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        byte[] bytes = objectMapper.writeValueAsBytes(obj);
//        String s = "abcdefg1111134234232423";
        byte sum  = 0;
        for (int i = 0, len = bytes.length; i < len; i++){
            sum += bytes[i];
        }
        // 求和
        System.out.println(sum);
        // 取反
        int ss = ~sum;
        System.out.println(ss);

        // 接收方校验
        int a = sum + ss + 1;
        System.out.println(a);
        return a == 0;
    }

}
